# converters/flatpak.py
import json
import subprocess

def convert_flatpak(wsl_file_path, temp_dir, log_callback):
    """将 Flatpak 包转换为玲珑包"""
    log_callback("开始转换 Flatpak 包...\n")
    
    # Step 1: 解压并解析元数据
    extract_dir = f"{temp_dir}/flatpak-extract"
    subprocess.run(f"wsl ostree checkout --repo={wsl_file_path} :/ {extract_dir}", shell=True, check=True)
    
    # Step 2: 解析 metadata.json
    with open(f"{extract_dir}/metadata.json") as f:
        metadata = json.load(f)
    
    # Step 3: 生成 linglong.yaml
    yaml_content = f"""
    package:
      id: com.flatpak.converted.{metadata['name']}
      version: {metadata['runtime-version']}
      runtime: org.deepin.Runtime/20/x86_64
      base: minimal
    files:
      - from: {extract_dir}/
        to: /
    """
    return yaml_content